﻿namespace EyeOpen.Web.Mvc.Modules
{
	using System.Web;
	using System.Web.Mvc;
	using System.Web.Routing;

	public abstract class HttpModule : IHttpModule
	{
		private static bool initialized;

		public virtual void Init(HttpApplication context)
		{
			if (initialized)
			{
				return;
			}

			AreaRegistration.RegisterAllAreas();
			RegisterGlobalFilters(GlobalFilters.Filters);
			RegisterRoutes(RouteTable.Routes);

			Initialize();

			initialized = true;
		}

		public virtual void Dispose()
		{
		}

		protected virtual void Initialize()
		{
		}

		protected virtual void RegisterCustomRoutes(RouteCollection routes)
		{
		}

		private void RegisterGlobalFilters(GlobalFilterCollection filters)
		{
			filters.Add(new HandleErrorAttribute());
		}

		private void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			RegisterCustomRoutes(routes);
		}
	}
}